﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Common;
using Microsoft.AspNetCore.Mvc;
using WqLunTan.Areas.Manage.Models;
using WqLunTan.Filters;
using WqLunTan.Models.DataBase;

namespace WqLunTan.Controllers
{
    /// <summary>
    /// 占卜
    /// </summary>
    public class DivinationController : BaseController
    {
        /// <summary>
        /// 免费预测
        /// </summary>
        /// <param name="forecast"></param>
        /// <returns></returns>
        [Auth]
        public IActionResult Add(article_forecast model)
        {
            if (model.born_date == DateTimeOffset.MinValue)
            {
                return JsonFailResult("出生时间参数错误");
            }

            try
            {
                model.user_id = LoginUser.Id;
                model.user_nick_name = LoginUser.NickName;
                model.create_time = DateTimeOffset.UtcNow;
                model.last_update = DateTimeOffset.UtcNow;
                DbContext.art_forecast.Add(model);
                int res = DbContext.SaveChanges();
                if (res > 0)
                {
                    var t1 = new DateTimeOffset(model.create_time.Date);
                    var count = DbContext.art_forecast.Where(p => p.create_time > t1).Count();
                    return JsonOkResult(string.Format("提交成功,您前面还有{0}位求测者,请耐心等待", count - 1));
                }
            }
            catch (Exception ex)
            {
                return JsonFailResult("服务器错误:" + ex.Message);
            }

            return JsonOkResult("提交成功");
        }

        /// <summary>
        /// 显示八字预测结果列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IActionResult List(string search, int pageIndex = 1, int pageSize = 20)
        {
            List<article_forecast> list = null;
            var page = new PageModel() { CurrentPageIndex = pageIndex };

            var query = DbContext.art_forecast.Where(x => x.is_show);

            if (string.IsNullOrWhiteSpace(search))
            {
                var key = $"{nameof(DivinationController)}-{nameof(DivinationController.List)}-{pageIndex}-{pageSize}";
                var pageKey= $"{nameof(DivinationController)}-{nameof(DivinationController.List)}-{pageIndex}-{pageSize}-page";
                list = httpCacheHelper.getCache<List<article_forecast>>(key);
                if (list != null && list.Count > 0)
                {
                    page = httpCacheHelper.getCache<PageModel>(pageKey);
                }
                else
                {
                    page.TotalCount = query.Count();
                    list = query.OrderByDescending(x => x.create_time).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
                    httpCacheHelper.setCache(key, list, 1);
                    httpCacheHelper.setCache(pageKey, page, 1);
                }
            }
            else
            {
                query = query.Where(x => x.horoscope_8char.Contains(search) || x.title.Contains(search));
                page.TotalCount = query.Count();
                list = query.OrderByDescending(x => x.create_time).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
            }

            ViewData["page"] = page;
            ViewData["list"] = list;

            return View();
        }

        /// <summary>
        /// 查看占卜结果
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Detail(Guid id)
        {
            var key = $"{nameof(DivinationController)}-{nameof(DivinationController.Detail)}-{id}";

            var art = httpCacheHelper.getCache<article_forecast>(key);
            if (art == null)
            {
                art = DbContext.art_forecast.Where(x => x.id == id && x.is_show).SingleOrDefault() ?? new article_forecast();
                httpCacheHelper.setCache(key, art, 1);
            }

            return View(art);
        }

    }
}